package icu.nil.demo.shell.interceptor

import icu.nil.demo.shell.config.USER_UUID_KEY
import icu.nil.demo.shell.config.logger
import org.springframework.http.server.ServerHttpRequest
import org.springframework.http.server.ServerHttpResponse
import org.springframework.http.server.ServletServerHttpRequest
import org.springframework.web.socket.WebSocketHandler
import org.springframework.web.socket.server.HandshakeInterceptor
import java.util.*


/**
 * Create by xiangliyou on 2020/3/10 16:27.
 * email: xiangliyou@aliyun.com
 * Des: websocket 拦截处理
 */
class ShellWebSocketInterceptor : HandshakeInterceptor {
    override fun afterHandshake(request: ServerHttpRequest, response: ServerHttpResponse,
                                wsHandler: WebSocketHandler, exception: Exception?) {
    }

    /**
     * 一个新的连接回话到来
     */
    override fun beforeHandshake(request: ServerHttpRequest, response: ServerHttpResponse,
                                 wsHandler: WebSocketHandler, attributes: MutableMap<String, Any>): Boolean {
        //生成一个UUID
        val uuid = UUID.randomUUID().toString().replace("-", "")
        //将uuid放到websocketsession中
        attributes[USER_UUID_KEY] = uuid

        return true
    }
}